package Me2day;

use strict;
use warnings;
use Encode;
use URI::Escape;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Digest::MD5;
use Data::Dumper;
use constant URL => "http://me2day.net/api/";

sub new {
    my ($class,%args) = @_;

    my $auth = {};

    while(my ($key, $row) = each %args) {
	$auth->{$key} = $row;
	Carp::croak($key. " argument is required") unless defined $auth->{$key};
    }

    my $self = {};
    $self->{username} = URI::Escape::uri_escape($auth->{username});
    $self->{password} = _createKey($auth->{user_key});
    $self->{doctype}  = $auth->{doctype} || "xml";
    bless $self, $class;
    $self;
}

sub _createKey {
    my $user_key = shift;

    return unless $user_key;

    my $nonce = sprintf("%08x", rand(16 ** 8));
    my $digest = Digest::MD5->new;
    $digest->add($nonce.$user_key);
    return $nonce.$digest->hexdigest;
}

sub _getURL { 
    my ($self, $name, $user) = @_;

    my $url = URL;
    if ($user) {
	$url .= $name."/".$user.".".$self->{doctype};
    } else {
	$url .= $name.".".$self->{doctype};
    }
    $url;
}

sub _transmit {
    my ($self, $method, $url, $args, $auth, $oops) = @_;

    my $ua = LWP::UserAgent->new;
    my $req = HTTP::Request->new( $method => $url );
    if ($args) {
	my @arguments;
	while(my ($key, $row) = each %{ $args }) {
	    next unless $key;
	    my $arg_name;
	    if ($oops && $key =~ /^(?:body|tags|icon)$/) {
		$arg_name = "post[${key}]";
	    } else {
		$arg_name = $key;
	    }
	    push @arguments, "$arg_name=".($row ? URI::Escape::uri_escape($row) : '');
	}
	my $content = join("&", @arguments);
	$req->content($content);
    }
    if ($auth) {
	$req->authorization_basic($self->{username}, $self->{password});
    }
    my $res = $ua->request($req);
    $res;
}

sub get_posts {
    my ($self, %args) = @_;

    my $url = $self->_getURL("get_posts", ($args{username} ||$self->{username}));
    foreach my $key (qw/from to tag/) {
	$args{$key} ||= '';
    }

    $self->_transmit("POST", $url, \%args);
}

sub get_comments {
    my ($self, %args) = @_;

    foreach my $key (qw/post_id/) {
	$args{$key} ||= '';
    }

    my $url = $self->_getURL("get_comments");
    $self->_transmit("GET", $url, \%args);
}

sub get_latests {
    my ($self, %args) = @_;

    my $url = $self->_getURL("get_latests", ($args{username} ||$self->{username}));
    $self->_transmit("GET", $url);
}

sub get_friends {
    my ($self, %args) = @_;

    my $url = $self->_getURL("get_friends", ($args{username} ||$self->{username}));
    foreach my $key (qw/scope/) {
	$args{$key} ||= '';
    }

    $self->_transmit("GET", $url, \%args);
}

sub get_person {
    my ($self, %args) = @_;

    my $url = $self->_getURL("get_person", ($args{username} ||$self->{username}));
    $self->_transmit("GET", $url);
}

sub get_settings {
    my $self = shift;

    my $url = $self->_getURL("get_settings");

    $self->_transmit("GET", $url, {}, 1);
}

sub get_tags {
    my $self = shift;

    #not defined
}

sub create_comment {
    my ($self, %args) = @_;

    my $url = $self->_getURL("create_comment");
    foreach my $key (qw/post_id body/) {
	$args{$key} ||= '';
    }
    $self->_transmit("POST", $url, \%args, 1);
}

sub create_post {
    my ($self ,%args) = @_;

    my $url = $self->_getURL("create_post");
    foreach my $key (qw/body tag icon kind receive_sms icon_url callback_url content_type longitude latitude/) {
	$args{$key} ||= '';
    }
    $self->_transmit("POST", $url, \%args, 1, "OOPS");
}

sub track_comments {
    my ($self, %args) = @_;
    
    my $url = $self->_getURL("track_comments", ($args{username} || $self->{username}));
    foreach my $key (qw/scope count/) {
	$args{$key} ||= '';
    }
    $self->_transmit("POST", $url, \%args);
}

sub noop {
    my $self = shift;
    my $url = $self->_getURL("noop");
    $self->_transmit("GET", $url, {}, 1);
}

1;
